package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.stat.StatMchDayBO;
import com.ymk.bo.stat.StatMchMonthBO;
import com.ymk.core.code.cmd.StatMchCmdCode;
import com.ymk.core.constants.SysConstants;
import com.ymk.service.IStatMchMonthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StatMchMonthPostCmd extends ACmd {

    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        StatMchMonthBO reqData = super.getData(sign, StatMchMonthBO.class);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");

        StatMchMonthBO statMchMonthBO = new StatMchMonthBO();
        statMchMonthBO.setMchId(reqData.getMchId());
        statMchMonthBO.setYear(reqData.getYear());
        statMchMonthBO.setMonth(reqData.getMonth());
        Date date = simpleDateFormat.parse(reqData.getYear()+"-"+reqData.getMonth());
        String dateDate = simpleDateFormat.format(date);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("mchId", reqData.getMchId())
                .addParam("date", dateDate);

        CmdSign cmdSign = CmdSign.Builder.newBuild(sign)
                .setSource(paramsBuilder.build())
                .setCmdCode(StatMchCmdCode.STAT_MCH_DAY_LIST)
                .setScope(SysConstants.CMD_SERVER_SCOPE)
                .build();
        //获取当月统计数据
        List<StatMchDayBO> statMchDayBOList = this.scheduler.schedule(request, cmdSign, StatMchDayBO.class, true);

        // 获取新增会员数量
        Integer memberCount = statMchDayBOList.stream().collect(Collectors.summingInt(StatMchDayBO::getMemberQuantity));
        statMchMonthBO.setMemberQuantity(memberCount);
        // 获取新增订单
        Integer ordersCount = statMchDayBOList.stream().collect(Collectors.summingInt(StatMchDayBO::getOrdersQuantity));
        statMchMonthBO.setOrdersQuantity(ordersCount);
        // 获取新增销量
        Map<Integer, BigDecimal> salesMap = statMchDayBOList.stream().collect(Collectors.groupingBy(StatMchDayBO::getId
                , Collectors.mapping(StatMchDayBO::getSales, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
        BigDecimal sales = new BigDecimal(0);
        for (Integer i : salesMap.keySet()){
            sales =  sales.add(salesMap.get(i));
        }
        statMchMonthBO.setSales(sales);
        // 获取新增销售金额
        Map<Integer, BigDecimal> payQuantityMap = statMchDayBOList.stream().collect(Collectors.groupingBy(StatMchDayBO::getId
                , Collectors.mapping(StatMchDayBO::getPayQuantity, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
        BigDecimal payQuantity = new BigDecimal(0);
        for (Integer i : payQuantityMap.keySet()){
            payQuantity =  payQuantity.add(payQuantityMap.get(i));
        }
        statMchMonthBO.setPayQuantity(payQuantity);
        this.setCreatedData(sign,statMchMonthBO);

        this.StatMchMonthService.post(statMchMonthBO);

        return respSign;
    }

    @Override
    public String getCmdCode() {
        return StatMchCmdCode.STAT_MCH_MONTH_POST;
    }

    @Autowired
    private IStatMchMonthService StatMchMonthService;


}